﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

namespace VisaValeCheck
{
    [Serializable()]
    public sealed class NumeroCartao
    {
        [XmlAttribute()]
        public string Numero { get; set; }
        public string NumeroFormatado { get { return Numero.Substring(0, 4) + "-" + Numero.Substring(4, 4) + "-" + Numero.Substring(8, 4) + "-" + Numero.Substring(12, 4); } }
        [XmlAttribute()]
        public bool Selecionado { get; set; }

        public NumeroCartao()
        {
        }

        public NumeroCartao(string numero)
        {
            Numero = numero.Replace("-", "");
            Selecionado = false;
            if (!Regex.Match(Numero, @"[\d]{16}").Success)
                throw new Exception("Número de cartão inválido.");
        }

        public override bool Equals(object obj)
        {
            return (obj is NumeroCartao) && ((NumeroCartao)obj == this);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public static bool operator ==(NumeroCartao a, NumeroCartao b)
        {
            return (object)a != null && (object)b != null && a.Numero == b.Numero;
        }

        public static bool operator !=(NumeroCartao a, NumeroCartao b)
        {
            return ((object)a == null && (object)b != null) || ((object)a != null && (object)b == null) || ((object)a != null && (object)b != null && a.Numero != b.Numero);
        }
    }
}
